<template>
  <div class="app-container">
    <div class="mainbox">
      <dt-table
        v-loading="loading"
        :table-opts="tableOpts"
        :pagination-data="{
          total:total,
          queryParams:queryParams
        }"
        @pagination="getList"
      />
      <!-- 添加或修改应用二次认证对话框 -->
      <dt-dialog :title="title" :visible.sync="open" :is-loading="isLoading"
                 @comfirmBtn="submitForm" @closeDialog="cancel">
        <div slot="content">
          <el-form ref="form" :model="form" :rules="rules"
                   label-width="110px">
            <el-form-item label="应用名称" prop="appName">
              <el-select v-model="appObj" value-key="appId" placeholder="请选择应用名称"
                         clearable size="small" style="width:100%;"
                         @change="selectAppName()">
                <el-option
                  v-for="item in ApplicationMagList"
                  :key="item.appId"
                  :label="item.appName"
                  :value="item"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="应用编码" prop="appCode">
              <el-input v-model.trim="form.appCode" disabled placeholder="请输入应用编码"
                        maxlength="30" show-word-limit class="limit" />
            </el-form-item>
            <el-form-item label="二次认证方式" prop="statusFlag">
              <el-radio-group v-model="form.statusFlag">
                <el-radio
                  v-for="dict in dict.type.SecondaryAuthWay"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                >{{ dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-form>
        </div>
      </dt-dialog>
    </div>
  </div>
</template>
<script>
import { listApp, getApp, delApp, addApp, updateApp } from '@/framework/api/authenticationCenter/appSecondaryAuth/appSecondaryAuth'
import { getAppMagList } from '@/framework/api/applicationMag/applicationMag'

export default {
  name: 'AppSecondaryAuth',
  dicts: ['SecondaryAuthWay'],
  data() {
    return {
      search: {},
      // 遮罩层
      loading: false,
      // 显隐表格
      isShowTable: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 应用管理列表
      ApplicationMagList: [],
      appObj: {},
      // 应用二次认证表格数据
      AppSecondaryAuthList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        appName: null,
        statusFlag: null
      },
      tableOpts: {
        rowKey: 'id',
        toolbarBtn: [
          {
            label: '新增',
            permission: ['APPSECONDARYAUTH_ADD_BUTTON'],
            type: 'primary',
            icon: 'el-icon-plus',
            click: () => {
              this.handleAdd()
            }
          }
        ],
        indexTableCol: {
          label: '序号',
          width: 70
        },
        tableCol: [
          {
            prop: 'appName',
            label: '应用名称',
            show: true,
            search: true
          },
          {
            prop: 'appCode',
            label: '应用编码',
            show: true
          },
          {
            prop: 'statusFlag',
            label: '二次认证方式',
            show: true,
            type: 'dict',
            search: true,
            formatter: () => { return this.dict.type.SecondaryAuthWay },
            dicData: [],
            labelWidth: '100px'
          },
          {
            prop: 'createTime',
            label: '创建时间',
            show: true
          }
        ],
        operator: {
          fixed: 'right',
          align: 'center',
          width: 246,
          operatorBtn: [
            {
              text: '修改',
              permission: ['APPSECONDARYAUTH_UPDATE_BUTTON'],
              icon: 'el-icon-edit',
              click: (row, col, cellValue) => {
                return this.handleUpdate(row)
              }
            },
            {
              text: '删除',
              permission: ['APPSECONDARYAUTH_DEL_BUTTON'],
              icon: 'el-icon-delete',
              type: 'danger',
              click: (row, col, cellValue) => {
                return this.handleDelete(row)
              }
            }
          ]
        },
        tableData: []
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        appName: [
          { required: true, message: '应用名称不能为空', trigger: 'change' }
        ],
        appCode: [
          { required: true, message: '应用编码不能为空', trigger: 'change' }
        ],
        statusFlag: [
          { required: true, message: '二次认证方式不能为空', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.initData()
  },
  methods: {
    initData() {
      this.getList()
      this.getAppMag()
    },

    /** 查询应用二次认证列表 */
    getList(query = {}) {
      this.loading = true
      listApp(this.queryParams).then(({ data: response }) => {
        this.AppSecondaryAuthList = response.rows
        this.total = response.totalRows
        this.loading = false
      })
    },
    // 获取应用列表
    getAppMag() {
      getAppMagList().then(({ data: response }) => {
        this.ApplicationMagList = response.rows
      })
    },
    selectAppName() {
      this.form.appName = this.appObj.appName
      this.form.appCode = this.appObj.appCode
    },
    // 取消按钮
    cancel() {
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        appName: null,
        appCode: null,
        statusFlag: null
      }
      this.appObj = {}
      this.resetForm('form')
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '新增'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      getApp(row.appId).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.appId != null) {
            updateApp(this.form).then((response) => {
              this.$dtModal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addApp(this.form).then((response) => {
              this.$dtModal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$dtModal.confirm(`是否确认删除应用二次认证名称为"${row.appName}"的数据项？`).then(() => {
        return delApp(row.appId)
      }).then(() => {
        this.getList()
        this.$dtModal.msgSuccess('删除成功')
      }).catch(() => {})
    }
  }
}
</script>

